import { assign, bind, extend } from 'lodash';
import Axios from './lib/core/Axios'
import defaults from './lib/defaults.js'

function createInstance(instanceConfig) {
	// 初始化Axios，含interceptors、request()
	let context = new Axios(instanceConfig);
	// 将request()中的this指向context,这样就可以在request()中取到context中的interceptors
	// function instance(config) {
	// 	function request(config) {
	// 		context.interceptors.request.forEach()
	// 	}
	// }
	let instance = bind(Axios.prototype.request, context)

	// 将context中的interceptors扩展到request()中，以便添加拦截器(调用拦截器的use函数)
	// function instance(config) {
	// 	context.interceptors = {
	// 		request: new InterceptorManager()
	// 	}

	// 	function request(config) {
	// 		context.interceptors.request.forEach()
	// 	}
	// }
	extend(instance, context)

	return instance;
}

let axios = createInstance(defaults);

axios.create = function create(instanceConfig) {
	return createInstance(assign(defaults, instanceConfig));
}

export default axios;